package com.example.administrator.yuejian35.Login.activity;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.administrator.yuejian35.Find.GlideLoader;
import com.example.administrator.yuejian35.Home.bean.Result;
import com.example.administrator.yuejian35.Login.model.LoginModel;
import com.example.administrator.yuejian35.MainActivity;
import com.example.administrator.yuejian35.R;
import com.justin.bieber.image.ImageConfig;
import com.justin.bieber.image.ImageSelector;
import com.justin.bieber.image.ImageSelectorActivity;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class RegActivity extends AppCompatActivity {
    private EditText et_uname;
    private EditText et_password;
    private EditText et_repassword;
    private Button button_reg;
    private CheckBox cb;
    private String uName;
    private String passwd;
    private ArrayList<String> path = new ArrayList<>();
    /*private String path;*/
    private ImageView btn2;
    private String mspassword;
    private ImageView iv;
    public static final int REQUEST_CODE = 123;

    private ImageConfig imageConfig;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_reg);
        et_uname = (EditText) findViewById(R.id.et_uname);
        et_password = (EditText) findViewById(R.id.et_password);
        et_repassword = (EditText) findViewById(R.id.et_repassword);

        cb = (CheckBox) findViewById(R.id.cb);
        btn2=(ImageView) findViewById(R.id.aaa);
        button_reg = (Button) findViewById(R.id.button_reg);
        iv = (ImageView) findViewById(R.id.video_pic);

        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                imageConfig = new ImageConfig.Builder(
                        new GlideLoader())
                        .steepToolBarColor(getResources().getColor(R.color.titleBlue))
                        .titleBgColor(getResources().getColor(R.color.titleBlue))
                        .titleSubmitTextColor(getResources().getColor(R.color.white))
                        .titleTextColor(getResources().getColor(R.color.white))
                        // 开启多选   （默认为多选）
                        .mutiSelect()
                        // 多选时的最大数量   （默认 9 张）
                        .mutiSelectMaxSize(1)
                        //设置图片显示容器，参数：、（容器，每行显示数量，是否可删除）
                        //.setContainer(llContainer, 1, true)
                        // 已选择的图片路径
                        .pathList(path)
                        // 拍照后存放的图片路径（默认 /temp/picture）
                        .filePath("/temp")
                        // 开启拍照功能 （默认关闭）
                        .showCamera()
                        .requestCode(REQUEST_CODE)
                        .build();
                ImageSelector.open(RegActivity.this, imageConfig);
            }
        });

        iv.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(RegActivity.this, MainActivity.class);
                startActivityForResult(intent,1);
            }
        });

        button_reg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                addPost(view);
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK && data != null) {
            List<String> pathList = data.getStringArrayListExtra(ImageSelectorActivity.EXTRA_RESULT);

            path.clear();
            path.addAll(pathList);

            File file = new File(path.get(0));
            if (file.exists()) {
                Bitmap bm = BitmapFactory.decodeFile(path.get(0));
                //将图片显示到ImageView中
                btn2.setImageBitmap(bm);
            }

        }
    }




    private void addPost(final View view) {

        File file = new File(path.get(0));

        RequestBody requestFile =
                RequestBody.create(MediaType.parse("multipart/form-data"), file);


        MultipartBody.Part body =
                MultipartBody.Part.createFormData("file", file.getName(), requestFile);

        uName = et_uname.getText().toString();
        passwd = et_password.getText().toString();
        mspassword = et_repassword.getText().toString();
        if(mspassword.equals(passwd)){
            if(cb.isChecked()){
                LoginModel loginmodel = new LoginModel();
                Call call = loginmodel.reg(uName,passwd,body);
                call.enqueue(new Callback<Result>() {
                    @Override
                    public void onResponse(Call<Result> call, Response<Result> response) {
                        Toast.makeText(RegActivity.this,"注册成功", Toast.LENGTH_LONG).show();
                        switch (view.getId()){
                            case R.id.button_reg://注册

                                String  username=uName;
                                Intent data = new Intent();
                                data.putExtra("name",username);
                                setResult(RESULT_OK,data);
                                finish();
                                break;

                        }
                        finish();
                    }

                    @Override
                    public void onFailure(Call<Result> call, Throwable t) {
                        Log.e("Upload error:", t.getMessage());
                        Toast.makeText(RegActivity.this,"注册失败", Toast.LENGTH_LONG).show();
                    }
                });
            }
            else{
                Toast.makeText(RegActivity.this,"未同意相关协议", Toast.LENGTH_LONG).show();
            }
        }
        else{
            Toast.makeText(RegActivity.this,"两次密码输入不一致", Toast.LENGTH_LONG).show();
        }
    /*    LoginModel loginmodel = new LoginModel();
        Call call = loginmodel.reg(uName,passwd,body);
        call.enqueue(new Callback<Result>() {
            @Override
            public void onResponse(Call<Result> call, Response<Result> response) {
                Toast.makeText(RegActivity.this,"注册成功", Toast.LENGTH_LONG).show();
                finish();
            }

            @Override
            public void onFailure(Call<Result> call, Throwable t) {
                Log.e("Upload error:", t.getMessage());
                Toast.makeText(RegActivity.this,"注册失败", Toast.LENGTH_LONG).show();
            }
        });*/


    }

}

